Skip to main content
Version: 1.0.16

CREATE DOMAIN

CREATE DOMAIN — Define a new domain

Synopsis

CREATE DOMAIN name [ AS ] data_type

[ COLLATE collation ]

[ DEFAULT expression ]

[ constraint [ ... ] ]

where constraint is:

[ CONSTRAINT constraint_name ]

{ NOT NULL | NULL | CHECK (expression) }

Description

CREATE DOMAIN creates a new domain. A domain is essentially a data type with optional constraints (restrictions on the set of allowed values). The user who defines a domain becomes its owner.

If a schema name is given (e.g., CREATE DOMAIN myschema.mydomain ...), the domain will be created in the specified schema. Otherwise, it will be created in the current schema. The domain name must be unique among types and domains in its schema.

Domains are primarily used to abstract common constraints on fields into a single location for easier maintenance. For example, several tables might contain email address columns, all requiring the same CHECK constraint to validate address syntax. Rather than setting up a constraint individually on each table, you can define a domain for this purpose.

To create a domain, you must have USAGE privilege on its underlying type.

Parameters

name

The name of the domain to be created (can be schema-qualified).

data_type

The underlying data type of the domain. This can include array specifiers.

collation

An optional collation for the domain. If no collation is specified, the underlying data type's default collation will be used. If COLLATE is specified, the underlying type must be collatable.

DEFAULT expression

The DEFAULT clause specifies a default value for columns of the domain data type. The value is any variable-free expression (but subqueries are not allowed). The data type of the default value expression must match the domain's data type. If no default is specified, the default value is null.

The default value expression will be used in any insert operation that does not specify a column value. If a default value is defined for a particular column, it overrides the default associated with the domain. In turn, the domain default overrides any default associated with the underlying data type.

CONSTRAINT constraint_name

An optional name for a constraint. If not specified, the system generates a name.

NOT NULL

Values of this domain are normally not allowed to be null (but see the notes below).

NULL

Values of this domain are allowed to be null. This is the default.

This clause is only intended for compatibility with non-standard SQL databases. Its use is discouraged in new applications.

CHECK (expression)

The CHECK clause specifies an integrity constraint or test that values of the domain must satisfy. Each constraint must be an expression producing a Boolean result. It should use the keyword VALUE to refer to the value being tested. Expressions evaluating to TRUE or UNKNOWN succeed. If the expression produces a FALSE result, an error is reported and the value is not allowed to be converted to the domain type.

Currently, CHECK expressions cannot contain subqueries and cannot reference variables other than VALUE.

When a domain has multiple CHECK constraints, they are tested in alphabetical order by name.

Notes

Domain constraints (particularly NOT NULL) are checked when a value is converted to the domain type. Even with such a constraint, it is possible for a column nominally of the domain type to be read as null. For example, if a column of the domain appears on the null-producing side of an outer join query. Here is a more subtle example:

INSERT INTO tab (domcol) VALUES ((SELECT domcol FROM tab WHERE false));

The empty scalar sub-SELECT produces a null value, which is considered to be of the domain type, so no further constraint checking is applied, and the insert succeeds.

It is difficult to avoid such problems, because SQL's general assumption is that null is a legal value for every data type. Therefore, the best approach is to design a domain constraint that allows null values, and then apply column NOT NULL constraints to columns of the domain type as needed.

The system assumes that CHECK constraint conditions are immutable, meaning they always produce the same result for the same input values. This assumption is only checked when first converting a value to the domain type, not at other times.

A common example of violating this assumption is referencing a user-defined function in a CHECK expression and then changing the function's behavior. The system does not prohibit this, but it also will not notice if stored values of the domain type violate the CHECK constraint in the meantime. This will cause subsequent database dumps and reloads to fail. The recommended approach for handling such changes is to drop the constraint (using ALTER DOMAIN), adjust the function definition, re-add the constraint, and then

re-check the constraint against the stored data.

Examples

This example creates the us_postal_code data type and uses it in a table definition. A regular expression test is used to verify that the value looks like a valid US postal code:

CREATE DOMAIN us_postal_code AS TEXT

CHECK(

VALUE ~ '^\d{5}$'

OR VALUE ~ '^\d{5}-\d{4}$'

);

CREATE TABLE us_snail_addy (

address_id SERIAL PRIMARY KEY,

street1 TEXT NOT NULL,

street2 TEXT,

street3 TEXT,

city TEXT NOT NULL,

postal us_postal_code NOT NULL

);

See Also

ALTER DOMAIN, DROP DOMAIN